package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarTripPower;
import com.navinfo.tripanalysis.common.util.BigDecimalUtils;
import com.navinfo.tripanalysis.common.arithmetic.common.VehicleInfo;

/**
 * 行程总功率算法<br/>
 * 依赖{@link VehicleInfo}的发动机输出最大扭距<br/>
 * 输出：统计<br/>
 */
public class TripPower extends AbstractArithmetic<VarTripPower> {
    /**
     * 系数K
     */
    static final double DEV = 100.0 * 9549.0;

    @Override
    public void calculate() {
        logger.debug("开始行程总功率算法[terminalId:{}]", terminalId);
        VehicleInfo vehicleInfo = drivingData.getVehicleInfo();
        if (vehicleInfo == null) {
            logger.debug("TripPower算法：获取车辆信息为空，返回");
            return;
        }
        double maxTorque = vehicleInfo.getEngineMaxTorque();
        if(maxTorque <= 0){
            logger.debug("TripPower算法：发动机最大扭矩为空，返回");
            return;
        }

        //计算总功率
        CommonData commonData = drivingData.getCommonData();
        double rotation = commonData.getRotation();
        double outputTorque = commonData.getEngineOutputTorque();
        if (outputTorque > 0) {
            VarTripPower obj= getObj();
            //功率：发动机最大扭矩*当前输出扭矩*当前转速
            double p = maxTorque * outputTorque * rotation;
            obj.setPower(p+ obj.getPower());
            logger.debug("TripPower算法：maxTorque{},outputTorque:{},rotation:{} ->{}",maxTorque, outputTorque, rotation, p);
        }else{
            logger.debug("TripPower算法：outputTorque<=0，返回");
            return;
        }
        logger.debug("结束行程总功率算法[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        //总功率：sum(发动机最大扭矩*当前输出扭矩*当前转速)/(100.0 * 9549.0)
        statisticDataOut.setTripPower(BigDecimalUtils.divide(getObj().getPower(), DEV,0).intValue());
    }
}
